#include "triu_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"
#include<stdio.h>

namespace optiling {
const int32_t BLOCK_DIM = 1;
const int32_t IO_CHANNELS = 4;
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
    TilingData tiling;
    context->SetBlockDim(BLOCK_DIM);

    int32_t totalLen = context->GetInputShape(0)->GetOriginShape().GetShapeSize();
    int32_t len_shape = context->GetInputShape(0)->GetOriginShape().GetDimNum();
    int32_t height = context->GetInputShape(0)->GetOriginShape().GetDim(len_shape-2);
    int32_t width = context->GetInputShape(0)->GetOriginShape().GetDim(len_shape-1);
    int32_t batchSize = totalLen/height/width;
    int32_t batchStride = height*width;

    const gert::RuntimeAttrs *attrs = context->GetAttrs();
    int32_t diagonal = *(attrs->GetAttrPointer<int32_t>(0));
    
    int32_t dtypeSizeX = ge::GetSizeByDataType(context->GetInputDesc(0)->GetDataType());
    printf("DTYPE_SIZE: %d\n", dtypeSizeX);
    int32_t blockLen = 32/dtypeSizeX;
    
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    uint64_t ubSize = 0; // ub剩余空间大小
    ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize);
    ubSize = ubSize/dtypeSizeX;
    int32_t bufferSize = (width+blockLen-1)/blockLen*blockLen;
    if(bufferSize*IO_CHANNELS>ubSize){
        bufferSize = ubSize/IO_CHANNELS;
        bufferSize -= bufferSize%blockLen;
    }
    tiling.set_height(height);
    tiling.set_width(width);
    tiling.set_diagonal(diagonal);
    tiling.set_batchSize(batchSize);
    tiling.set_batchStride(batchStride);
    tiling.set_bufferSize(bufferSize);
    tiling.set_blockLen(blockLen);
    tiling.set_totalLen(totalLen);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    currentWorkspace[0] = 0;
    
    return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class Triu : public OpDef {
public:
    explicit Triu(const char* name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("diagonal").AttrType(OPTIONAL).Int(0);

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc)
            .AddConfig("ascend310b");
    }
};

OP_ADD(Triu);
}
